package com.woniuxy.finance.controller;

import com.alibaba.excel.EasyExcel;
import com.woniuxy.commons.utils.ResponseResult;
import com.woniuxy.finance.entity.Settlement;
import com.woniuxy.finance.entity.Transfer;
import com.woniuxy.finance.service.SettlementService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.List;

/**
 * @ClassName SettlementController
 * @Description TODO
 * @Author WXY
 * @Date 2022/6/10 14:24
 * @Version 1.0
 */
@RestController
@RequestMapping("/finance/settlement")
public class SettlementController {

    @Resource
    private SettlementService settlementService;

    @GetMapping("/")
    public ResponseResult findAll(){
        return settlementService.findAll();
    }

    @GetMapping("/week")
    public ResponseResult<List<Settlement>> findAllByWeek(){
        return settlementService.findAllByWeek();
    }
    @GetMapping("/month")
    public ResponseResult<List<Settlement>> findAllByMonth(){
        return settlementService.findAllByMonth();
    }
    @GetMapping("/date/{date}")
    public ResponseResult<List<Settlement>> findAllByDate(@RequestBody String date){
        return settlementService.findAllByDate(date);
    }
    @GetMapping("/unsettleCostSum")
    public BigDecimal findUnsettleCostSum(){return settlementService.unsettleCostSum();}
    @GetMapping("/export")
    @ApiOperation(value = "导出excel")
    public void exportExcel(HttpServletResponse response) throws IOException {
        // 这里URLEncoder.encode可以防止中文乱码，所有通过后端的文件下载都可以如此处理
        String fileName = URLEncoder.encode("结算.xlsx", "UTF-8");
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf8");
        //建议加上该段，否则可能会出现前端无法获取Content-disposition
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        EasyExcel.write(response.getOutputStream(), Settlement.class).sheet("模板")
                .doWrite(settlementService.findAll().getData());
    }
}
